import java.util.ArrayList;

public class L707 {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addAtHead(1);
        myLinkedList.get(0);
        myLinkedList.addAtTail(3);
        myLinkedList.addAtIndex(1, 2);
        myLinkedList.get(1);
        myLinkedList.deleteAtIndex(1);
        myLinkedList.get(1);


//        ArrayList<String> strings = new ArrayList<>(2);
//        strings.add("qwe");
//        strings.add("dd");
//        ArrayList<String> strings1 = strings;
//        strings1.add("123");
//        System.out.println(strings.toString());
//        System.out.println(strings1.toString());


    }


    static class MyLinkedList {
        int size;
        ListNode head;


        public MyLinkedList() {
            size = 0;
            head = new ListNode(0);

        }

        public int get(int index) {
            if (index < 0 || index >= size) {
                return -1;
            }
            ListNode currentNode = this.head;
            for (int i = 0; i <= index; i++) {
                currentNode = currentNode.next;
            }
            return currentNode.val;

        }

        public void addAtHead(int val) {
            addAtIndex(0, val);
        }

        public void addAtTail(int val) {
            addAtIndex(size, val);

        }

        public void addAtIndex(int index, int val) {
            if (index > size) {
                return;
            }
            if (index < 0) {
                index = 0;
            }
            ListNode pred = this.head;
            for (int i = 0; i < index; i++) {
                pred = pred.next;
            }
            ListNode listNode = new ListNode(val);
            listNode.next = pred.next;
            pred.next = listNode;

            size++;

        }

        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            if (index == 0) {
                head = head.next;
                return;
            }
            ListNode node = this.head;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            node.next = node.next.next;

        }

    }
}
